﻿Imports FairShop
Imports FairShop.Objects
Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.IO
Imports System.Xml.Schema
Imports System.Xml.Linq
Imports System.Xml

<TestClass()> _
Public Class TestVerkoopDocument

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    <ClassInitialize()> _
    Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
        AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory)
    End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> _
    Public Sub ValideerLayoutFilesTest()
        Dim folder As DirectoryInfo = GetResourcesFolder()

        Dim schemas As XmlSchemaSet = New XmlSchemaSet()
        schemas.Add(Nothing, XmlReader.Create(Path.Combine(folder.FullName, "verkoop.xsd")))

        For Each floFile In folder.GetFiles("*.flo")
            Console.WriteLine("Validating file {0}", floFile.Name)
            Dim xmlFile As XDocument = XDocument.Load(floFile.FullName)
            xmlFile.Validate(schemas, AddressOf ValidationCallBack)
        Next

        ' TODO: testen dat titelrij en sjabloonrij altijd evenveel cellen hebben
    End Sub

#Region "Helperfuncties"

    ' Display any validation errors.
    Private Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
        Assert.Fail("Validation error: {0}", e.Message)
    End Sub

    Private Function GetResourcesFolder() As DirectoryInfo
        Dim diTestDeployFolder As New DirectoryInfo(TestContext.TestDir)
        Dim diSolutionRoot As DirectoryInfo = diTestDeployFolder.Parent.Parent
        Dim diProjectFolder As DirectoryInfo = diSolutionRoot.GetDirectories("FairShop")(0)
        Dim diResourcesFolder As DirectoryInfo = diProjectFolder.GetDirectories("Resources")(0)
        Return diResourcesFolder
    End Function

#End Region

End Class